package com.zl.controller;

import com.github.pagehelper.PageInfo;
import com.zl.model.RoleDto;
import com.zl.model.common.ResponseBean;
import com.zl.model.entity.Permission;
import com.zl.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * RoleController
 * @author zhoulang
 * @date 2018/8/29 15:45
 */
@RestController
@RequestMapping("role")
public class RoleController {


    @Autowired
    private IRoleService iRoleService;

    /**
     * 分页查询
     * @param page 分页大小
     * @param rows 分页数量
     * @param name 名称
     * @return ResponseBean
     */
    @PostMapping(value = "/list")
    public ResponseBean select(@RequestParam(name = "page", defaultValue = "1") int page,
                               @RequestParam(name = "rows", defaultValue = "5") int rows,
                               @RequestParam(required = false) String name) {
        PageInfo pageInfo = iRoleService.selectPage(page, rows, name);
        return new ResponseBean(HttpStatus.OK.value(), "获取角色列表成功", pageInfo);
    }

    /**
     * 查询所有
     * @return ResponseBean
     */
    @GetMapping(value = "/listAll")
    public ResponseBean selectAll() {
        List<RoleDto> list = iRoleService.selectAll();
        return new ResponseBean(HttpStatus.OK.value(), "获取角色列表成功", list);
    }

    /**
     * 根据角色获取相应的功能
     * @param id 角色id
     * @return
     */
    @GetMapping(value = "/menulist/{id}")
    public ResponseBean menuList(@PathVariable int id) {
        Map<String,Object> list = iRoleService.menuList(id);
        return new ResponseBean(HttpStatus.OK.value(), "获功能列表成功", list);
    }

    /**
     * 根据ids[]获取相应的功能
     * @param data
     * @return
     */
    // TODO: 2021/3/23 0023
    @GetMapping(value = "/menuupdate")
    public ResponseBean menuUpdate(@RequestParam String[] data){
        return new ResponseBean(HttpStatus.OK.value(), "修改权限列表成功", "");
    }
    /**
     * 修改角色信息
     * @param roleDto 角色实体
     * @return ResponseBean
     */
    @PostMapping(value = "/update")
    public ResponseBean update(@RequestBody RoleDto roleDto) {
        iRoleService.updateByPrimaryKey(roleDto);
        return new ResponseBean(HttpStatus.OK.value(), "修改角色信息成功", "");
    }

    /**
     * 角色保存
     * @param roleDto 角色实体类
     * @return ResponseBean
     */
    @PostMapping(value = "/save")
    public ResponseBean save(@RequestBody RoleDto roleDto) {
        iRoleService.insert(roleDto);
        return new ResponseBean(HttpStatus.OK.value(), "插入角色成功", "");
    }

    /**
     * 角色的删除
     * @param ids 角色的id数组
     * @return ResponseBean
     */
    @PostMapping(value = "/delete")
    public ResponseBean delete(@RequestParam int ids[]) {
        iRoleService.deletes(ids);
        return new ResponseBean(HttpStatus.OK.value(), "删除角色成功", "");
    }


}
